<template>
  <div class="scale-box" :id="dynamicIdName()">
    <div
      class="hall-carousel-box animate__animated"
      :class="dynamicClassName"
      style="width: 880px;height: 367px;"
      :id="dynamicChildName()"
    >
      <div class="carousel-wrap">
        <div class="circle">
          <span>业务分析</span>
        </div>
        <ul
          class="carousel"
          id="carousel"
          @mouseenter="handleMouseEnter"
          @mouseleave="handleMouseLeave"
        >
          <li class="item " @mouseenter="handleMouseEnter">
            <span class="name">咨询量</span>
            <p class="num"><em>688</em>件</p>
          </li>
          <li class="item " @mouseenter="handleMouseEnter">
            <span class="name">预约量</span>
            <p class="num"><em>688</em>件</p>
          </li>
          <li class="item " @mouseenter="handleMouseEnter">
            <span class="name">取号量</span>
            <p class="num"><em>688</em>件</p>
          </li>
          <li class="item " @mouseenter="handleMouseEnter">
            <span class="name">审批受理</span>
            <p class="num"><em>688</em>件</p>
          </li>
          <li class="item " @mouseenter="handleMouseEnter">
            <span class="name">审批发证</span>
            <p class="num"><em>688</em>件</p>
          </li>
          <li class="item " @mouseenter="handleMouseEnter">
            <span class="name">审批发证</span>
            <p class="num"><em>688</em>件</p>
          </li>
          <li class="item " @mouseenter="handleMouseEnter">
            <span class="name">审批发证</span>
            <p class="num"><em>688</em>件</p>
          </li>
          <li class="item " @mouseenter="handleMouseEnter">
            <span class="name">审批发证</span>
            <p class="num"><em>688</em>件</p>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>
<script>
//引入封装方法
import encapsulation from "../../utils/aircimGird";
export default {
  mixins: [encapsulation],
  data() {
    return {
      raf: null,
      resizeObserver: null,
      //动态Id
      timestamp: null,
    };
  },
  mounted() {
    this.initCarousel();
    this.timestamp = new Date().getTime();
    this.$nextTick(() => {
      // 监听盒子变化
      this.listenChangeFn();
    });
  },
  methods: {
    // 初始化轮播图
    initCarousel() {
      const that = this;

      const domcarousel = document.getElementById("carousel");
      let translate = 0;
      const autoPlay = function() {
        translate += 0.1;
        if (translate === 360) translate = 0;
        that.setCircle(translate, domcarousel, 55); // 310
        that.raf = requestAnimationFrame(autoPlay);
      };
      autoPlay();
    },
    setCircle(translate, $dom, gap) {
      let len = $dom.querySelectorAll(".item").length,
        rotate = 360 / len,
        itemWidth = 140, // 元素宽度
        translatZ =
          (itemWidth + 2 * gap) / 2 / Math.tan((rotate * Math.PI) / 360);
      for (var i = 0; i < len; i++) {
        let deg = rotate * i + translate || 0,
          transX = Math.sin((deg / 180) * Math.PI) * translatZ,
          transZ = Math.cos((deg / 180) * Math.PI) * translatZ;

        let item = $dom.querySelectorAll(".item")[i];
        item.style.transform =
          "translateX(" + transX + "px) translateZ(" + transZ + "px)";
        item.style.opacity = (transZ + 250) / 250; // 控制远处点透明度
      }
    },
    handleMouseEnter() {
      cancelAnimationFrame(this.raf);
    },
    handleMouseLeave() {
      this.raf = null;
      this.initCarousel();
    },
    //监听外部盒子的大小 然后对数据井行缩放
    scaleFn() {
      const parentBox = document.getElementById(`dynamic-${this.timestamp}`);
      const childBox = document.getElementById(
        `dynamicChild-${this.timestamp}`
      );
      const parentWidth = parentBox.offsetWidth;
      const parentHeight = parentBox.offsetHeight;
      const childWidth = childBox.offsetWidth;
      const childHeight = childBox.offsetHeight;
      let scaleX, scaleY;
      if (parentWidth < childWidth || parentHeight < childHeight) {
        const scaleBasedOnWidth = parentWidth / childWidth;
        const scaleBasedOnHeight = parentHeight / childHeight;
        scaleX = scaleBasedOnWidth;
        scaleY = scaleBasedOnHeight;
      } else {
        scaleX = parentWidth / childWidth;
        scaleY = parentHeight / childHeight;
      }
      childBox.style.transform = `scale(${scaleX}, ${scaleY})`;
    },
    // 监听盒子变化
    listenChangeFn() {
      let that = this;
      const element = document.getElementById(`dynamic-${this.timestamp}`);
      // console.log(element, "element");
      if (element) {
        new ResizeObserver(() => {
          // 处理元素大小变化的逻辑
          that.scaleFn();
        }).observe(element);
      }
    },
    // 绑定动画样式 animate__flash
    bindAnimationFn(animateName) {
      if (animateName != null) {
        this.dynamicClassName = animateName;
      }
    },
    //定义唯一ID名
    dynamicIdName() {
      return `dynamic-${this.timestamp}`;
    },
    //定义唯一ID名
    dynamicChildName() {
      return `dynamicChild-${this.timestamp}`;
    },
  },
  // 销毁监听
  beforeDestroy() {
    // 组件销毁前停止监听
    if (this.resizeObserver) {
      this.resizeObserver.disconnect();
    }
    if (this.requestTime != null) {
      clearInterval(this.requestTime);
    }
  },
};
</script>
<style lang="scss" scoped>
@import "../../static/assets/common.scss";
</style>
<style lang="scss" scoped>
.scale-box {
  width: 100%;
  height: 100%;
}

.hall-carousel-box {
  width: 880px;
  height: 367px;
  position: absolute;
  padding: 0 20px;
  box-sizing: border-box;
  overflow: hidden;
  background-size: 100% 100%;
  background-repeat: no-repeat;

  .grid-charts {
    width: 100%;
    height: 100%;
  }
}

.carousel-wrap {
  width: 100%;
  height: 100%;
  position: relative;
  background-image: url(../../static/assets/carousel/bottom.png);
  background-position: center bottom;
  background-repeat: no-repeat;

  * {
    box-sizing: border-box;
  }

  .circle {
    position: absolute;
    inset: 0;
    background-image: url(../../static/assets/carousel/circle.png);
    background-repeat: no-repeat;
    background-position: center -140px;

    span {
      top: 96px;
      position: absolute;
      left: 0;
      width: 100%;
      font-family: YouSheBiaoTiHei;
      font-size: 34px;
      color: #fff;
      text-align: center;
    }
  }
}

.carousel {
  color: #fff;
  position: absolute;
  left: 330px;
  top: 112px;
  height: 50%;
  perspective: 3700px;
  perspective-origin: 50% -500%;
  transform-style: preserve-3d;

  .item {
    display: flex;
    flex-direction: column;
    width: 140px;
    height: 107px;
    background-image: url(../../static/assets/carousel/item.png);
    background-size: 100% 100%;
    text-align: center;
    overflow: hidden;
    flex-shrink: 0;
    position: absolute;

    cursor: pointer;

    .name {
      color: #fff;
      font-size: 18px;
      line-height: 27px;
      padding-top: 7px;
    }

    .num {
      color: #96bee0;
      font-size: 15.4px;

      em {
        padding-right: 2px;
        color: #0ff;
        font-style: normal;
        text-align: center;
        font-family: D-DIN;
        font-size: 26.4px;
        font-style: normal;
        font-weight: 700;
      }
    }
  }
}
</style>
<style>
body {
  background-color: rgba(6, 27, 38);
  background-image: url(../../../thumbnail/backImg/bg.png);
  background-position: center center;
}
</style>
